package longrise.phone.com.bjjt_henan.compensate;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.LoadDataManager;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.UiUtils;

/**
 * 有争议，无争议选择页面，Created by Administrator on 2016/6/2.
 */
public class DisputeView extends LFView implements View.OnClickListener {

    private Context context = null;
    private View view = null;
    private BaseApplication mApplication;
    private LinearLayout hn_btn_back;
    private TextView tv_title;
    private String appCaseno;
    private Button bt_disputereport;
    private Button bt_disputereport2;
    private TextView tv_disputereport1;
    private TextView tv_disputereport2;
    private TextView tv_disputereport3;
    private TextView tv_disputereport4;
    private TextView tv_disputereport5;
    private TextView tv_disputereport6;
    private RadioGroup rg_createdispute1;
    private RadioButton rb_createdispute1;
    private RadioButton rb_createdispute2;
    private RadioButton rb_createdispute3;
    private RadioGroup rg_createdispute2;
    private RadioButton rb_createdispute4;
    private RadioButton rb_createdispute5;
    private RadioButton rb_createdispute6;
    private Dialog processDialog;
    private EntityBean[] DrivingLicenceBeans;
    private String myCarType;
    private String myCarFrameNo;
    private String myCarPic;
    private EntityBean myDrivingLiscenceBean;
    private EntityBean[] infoBean;
    private String hisDuty;
    private String myDuty;

    public DisputeView(Context c, EntityBean myDrivingLiscenceBean, EntityBean[] DrivingLicenceBeans, EntityBean[] infoBean) {
        super(c);
        this.context = c;
        this.mApplication = (BaseApplication) c.getApplicationContext();
        this.DrivingLicenceBeans = DrivingLicenceBeans;
        this.myDrivingLiscenceBean = myDrivingLiscenceBean;
        this.infoBean = infoBean;

    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);


    }

    private void initView() {
        view = View.inflate(context, R.layout.view_dispute, null);
        bt_disputereport = (Button) view.findViewById(R.id.bt_disputereport);
        bt_disputereport2 = (Button) view.findViewById(R.id.bt_disputereport2);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_disputereport1 = (TextView) view.findViewById(R.id.tv_disputereport1);//对方姓名
        tv_disputereport2 = (TextView) view.findViewById(R.id.tv_disputereport2);//车牌号
        tv_disputereport3 = (TextView) view.findViewById(R.id.tv_disputereport3);//电话
        tv_disputereport1.setText(infoBean[0].getString("carownname"));
        tv_disputereport2.setText(infoBean[0].getString("casecarno"));
        tv_disputereport3.setText(infoBean[0].getString("casecarphone"));
        rg_createdispute1 = (RadioGroup) view.findViewById(R.id.rg_createdispute1);
        rb_createdispute1 = (RadioButton) view.findViewById(R.id.rb_createdispute1);
        rb_createdispute2 = (RadioButton) view.findViewById(R.id.rb_createdispute2);
        rb_createdispute3 = (RadioButton) view.findViewById(R.id.rb_createdispute3);
        tv_disputereport4 = (TextView) view.findViewById(R.id.tv_disputereport4);//本方姓名
        tv_disputereport5 = (TextView) view.findViewById(R.id.tv_disputereport5);//车牌号
        tv_disputereport6 = (TextView) view.findViewById(R.id.tv_disputereport6);//电
        tv_disputereport4.setText(infoBean[1].getString("carownname"));
        tv_disputereport5.setText(infoBean[1].getString("casecarno"));
        this.appCaseno = infoBean[1].getString("appCaseNo");
        tv_disputereport6.setText(mApplication.getLoginInfo().getBean("userinfo").getString("mobilephone"));
        rg_createdispute2 = (RadioGroup) view.findViewById(R.id.rg_createdispute2);
        rb_createdispute4 = (RadioButton) view.findViewById(R.id.rb_createdispute4);
        rb_createdispute5 = (RadioButton) view.findViewById(R.id.rb_createdispute5);
        rb_createdispute6 = (RadioButton) view.findViewById(R.id.rb_createdispute6);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("责任认定");
    }

    private void regEvent(boolean b) {
        if (null != bt_disputereport) {
            bt_disputereport.setOnClickListener(b ? this : null);
        }
        if (null != bt_disputereport2) {
            bt_disputereport2.setOnClickListener(b ? this : null);
        }

        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
        //对方责任选项
        if (null != rg_createdispute1) {
            rg_createdispute1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    switch (i) {
                        case R.id.rb_createdispute1:
                            hisDuty = "0";
                            rb_createdispute5.setChecked(true);
                            break;
                        case R.id.rb_createdispute2:
                            hisDuty = "1";
                            rb_createdispute4.setChecked(true);
                            break;
                        case R.id.rb_createdispute3:
                            hisDuty = "2";
                            rb_createdispute6.setChecked(true);
                            break;
                        case R.id.dispute_rb_other_main://主责
                            hisDuty = "3";
                            ((RadioButton) view.findViewById(R.id.dispute_rb_my_next)).setChecked(true);
                            break;
                        case R.id.dispute_rb_other_next://次责
                            hisDuty = "4";
                            ((RadioButton) view.findViewById(R.id.dispute_rb_my_main)).setChecked(true);
                            break;
                    }
                }
            });
        }
        //本方责任选项
        if (null != rg_createdispute2) {
            rg_createdispute2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    switch (i) {
                        case R.id.rb_createdispute4:
                            myDuty = "0";
                            rb_createdispute2.setChecked(true);
                            break;
                        case R.id.rb_createdispute5:
                            myDuty = "1";
                            rb_createdispute1.setChecked(true);
                            break;
                        case R.id.rb_createdispute6:
                            myDuty = "2";
                            rb_createdispute3.setChecked(true);
                            break;
                        case R.id.dispute_rb_my_main://主责
                            myDuty = "3";
                            ((RadioButton) view.findViewById(R.id.dispute_rb_other_next)).setChecked(true);
                            break;
                        case R.id.dispute_rb_my_next://次责
                            myDuty = "4";
                            ((RadioButton) view.findViewById(R.id.dispute_rb_other_main)).setChecked(true);
                            break;
                    }
                }
            });
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;
            case R.id.bt_disputereport:
                //无争议，自己定责，事故上传成功时生成协议书
                if (null == hisDuty) {
                    UiUtil.showToast(context, "请选择责任类型");
                    return;
                }
                //弹出dialog，二次确认
                //弹出照片上传正常的dialog
                UiUtils.showDialog(context, null, null, "确定提交吗？", "返回", "确认",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                return;
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                submitAccidentCase(1);
                            }
                        });
                break;
            case R.id.bt_disputereport2:
                //有争议交警定责
                if (null == hisDuty) {
                    UiUtil.showToast(context, "请选择责任类型");
                    return;
                }
                UiUtils.showDialog(context, null, null, "确定提交吗？", "返回", "确认",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                return;
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                submitAccidentCase(2);
                            }
                        });
                break;

        }
    }

    private void createAgreementByHand() {
        EntityBean hisBean = new EntityBean();
        hisBean.set("ownother", "1");
        hisBean.set("carownphone", tv_disputereport3.getText().toString().trim());
        hisBean.set("dutytype", hisDuty);
        hisBean.set("carownname", tv_disputereport1.getText().toString().trim());
        hisBean.set("casecarno", tv_disputereport2.getText().toString().trim());

        EntityBean myBean = new EntityBean();
        myBean.set("ownother", "0");
        myBean.set("carownphone", tv_disputereport6.getText().toString().trim());
        myBean.set("dutytype", myDuty);
        myBean.set("carownname", tv_disputereport4.getText().toString().trim());
        myBean.set("casecarno", tv_disputereport5.getText().toString().trim());
        EntityBean[] accidentBean = new EntityBean[]{hisBean, myBean};
        CreateAgreementView createagreementview = new CreateAgreementView(context, accidentBean, appCaseno);
        FrameworkManager.getInstance().showNewForm(context, createagreementview);
    }

    //有争议提交数据
    private void submitAccidentCase(final int createType) {
        processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
        EntityBean bean = mApplication.getKckpRequestBean();
        EntityBean userinfo = mApplication.getLoginInfo().getBean("userinfo");
        final String myCarNo = tv_disputereport5.getText().toString().trim();
        // caseno
        bean.set("appcaseno", appCaseno);
        bean.set("casecarno", myCarNo);
        String phoneNumber = userinfo.getString("mobilephone");
        bean.set("appphone", phoneNumber);
        // 经度
        bean.set("caselon", mApplication.getLongitude());
        // 纬度
        bean.set("caselat", mApplication.getLatitude());
        // 地址
        bean.set("accidentplace", mApplication.getAddress());
        // 提交时间
        bean.set("sibmitdate", CommontUtils.getNowTime());
        //bean.set("areaid", userinfo.getString("areaid")); // 区域
        bean.set("areaid", mApplication.getAreaid()); // 调试数据
        bean.set("alarmtime", CommontUtils.getNowTime());
        bean.set("token", mApplication.getToken());
        bean.set("accidenttype", infoBean[1].getString("accidentType"));
        bean.set("accidentdes", infoBean[1].getString("discription"));
        bean.set("disposetype", createType + "");//增加一个自己还是交警定责的类型字段
        List<String> carTypeCode = getCarTypeCode(myCarNo);
        if (null != carTypeCode) {
            myCarType = carTypeCode.get(0);
            myCarFrameNo = carTypeCode.get(1);
            myCarPic = carTypeCode.get(2);
        }
        EntityBean[] casecarlist = null;//车主信息数组
        EntityBean bfBean = new EntityBean();// 本方数据
        String myName = tv_disputereport4.getText().toString().trim(); // 车主姓名
        bfBean.set("carownname", myName);
        bfBean.set("carownphone", phoneNumber); //手机号
        bfBean.set("driverlicence", infoBean[1].getString("casecarId")); //
        bfBean.set("casecarno", tv_disputereport5.getText().toString().trim()); // 案件车牌号
        bfBean.set("cartype", myCarType); // 车型
        bfBean.set("frameno", myCarFrameNo);//车架号
        bfBean.set("cardno", infoBean[1].getString("casecarId")); //身份证
        bfBean.set("driverfileno", myDrivingLiscenceBean.getString("driverfileno")); //档案编号
        bfBean.set("drivernoimg", myDrivingLiscenceBean.getString("drivernoimg")); //驾驶证照片
        bfBean.set("drivingnoimg", myCarPic);  //行驶证照片
        EntityBean dfBean = new EntityBean();// 对方数据
        dfBean.set("carownname", tv_disputereport1.getText().toString().trim()); // 车主姓名
        dfBean.set("carownphone", tv_disputereport3.getText().toString().trim());  // 电话
        dfBean.set("casecarno", tv_disputereport2.getText().toString().trim());  // 案件车牌号
        dfBean.set("cartype", infoBean[0].getString("carType")); // 车型
        dfBean.set("driverlicence", infoBean[0].getString("casecarId")); //
        dfBean.set("frameno", infoBean[0].getString("frameno")); //车架号
        if (createType == 1) {
            bfBean.set("dutytype", myDuty);
            dfBean.set("dutytype", hisDuty); //当自己解决时增加一个责任类型的字段
        }

        casecarlist = new EntityBean[]{bfBean, dfBean};
        bean.set("casecarlist", casecarlist);
        LoadDataManagerCar.getInstance(context).callService(null,
                mApplication.getServerUrl(), URLConstant.SUBMITCASEINFOR, bean,
                new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        String redes = bean.getString("redes");
                        if ("1".equals(restate)) {
                            UiUtil.showToast(context, "提交成功");
                            if (createType == 1) {
                                //自行解决进入生成协议书
                                createAgreementByHand();
                            } else {
                                //进入提交成功
                                AccidentLoadingSuccessView accidentloadingsuccessview = new AccidentLoadingSuccessView(context, appCaseno);
                                FrameworkManager.getInstance().showNewForm(context, accidentloadingsuccessview);
                            }
                        } else {
                            UiUtil.showToast(context, redes);
                        }
                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        if (resultType == LoadDataManagerFather.ResultType.Failure) {
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        } else if (resultType == LoadDataManagerFather.ResultType.NoNetWorks) {
                            UiUtil.showToast(context, "无网络连接，请检查您的网络");
                        }
                        return;
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                    }
                });
    }

    /**
     * 根据车牌获取获取车型，车架号，行驶证照片
     *
     * @param carNo
     * @return
     */
    private List<String> getCarTypeCode(String carNo) {
        if (TextUtils.isEmpty(carNo)) {
            return null;
        }
        List<String> myCarInfo = new ArrayList<String>();
        String value1 = "";
        String value2 = "";
        String value3 = "";
        for (EntityBean bean : DrivingLicenceBeans) {
            String getCarInfo = bean.getString("carno");
            if (carNo.equals(getCarInfo)) {
                value1 = bean.getString("cartype");//车型
                myCarInfo.add(value1);
                value2 = bean.getString("identificationnum");//车架号
                myCarInfo.add(value2);
                value3 = bean.getString("drivingnoimg");//行驶证照片
                myCarInfo.add(value3);
                break;
            }
        }
        return myCarInfo;
    }
}
